package cn.aib.eduservice.controller;


import cn.aib.commonutils.R;
import cn.aib.eduservice.entity.EduTeacher;
import cn.aib.eduservice.entity.vo.TeacherQuery;
import cn.aib.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-14
 */
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    //依赖EduTeacherService
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<EduTeacher> eduTeacherList = eduTeacherService.list(null);

        return R.ok().data("items", eduTeacherList);
    }

    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){
        boolean result = eduTeacherService.removeTeacherById(id);
        //判断结果
        if(result){
            return R.ok();
        }else {
            return R.error();
        }

    }

    /**
     * 讲师分页查询
     * @param current：当前页
     * @param limit：每页记录数
     * @return
     */
    @GetMapping("{current},{limit}")
    public R getPageTeacher(@PathVariable long current,@PathVariable long limit){
        //创建page对象，该对象封装了所有的分页数据
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //查询数据库数据和分页信息并封装到page对象中;第二个参数为null，代表条件为空
        eduTeacherService.page(pageTeacher, null);

        //获取总记录数和分页数据
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        //创建一个map
        Map<String, Object> map = new HashedMap<>();
        //放入数据到map
        map.put("total", total);
        map.put("records", records);

        //返回
        return R.ok().data(map);
    }

    /**
     * 讲师多条件组合分页查询
     * @param current：当前页
     * @param limit：每页记录数
     * @param teacherQuery：封装的条件对象
     * @return
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                            @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象，该对象封装了所有的分页数据
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //创建wrapper对象，判断属性是否为空添加条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        //取teacherQuery的属性值
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //判断进行拼接条件
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level", level);
        }

        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create", begin);
        }

        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified", end);
        }

        //根据创建时间降序排序
        queryWrapper.orderByDesc("gmt_create");

        //查询数据库数据和分页信息并封装到page对象中;第二个参数为null，代表条件为空
        eduTeacherService.page(pageTeacher, queryWrapper);

        //获取总记录数和分页数据
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        //创建一个map
        Map<String, Object> map = new HashedMap<>();
        //放入数据到map
        map.put("total", total);
        map.put("records", records);

        //返回
        return R.ok().data(map);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        //添加讲师
        boolean result = eduTeacherService.saveTeacher(teacher);
        //判断结果并返回
        if(result){
            return R.ok();
        }else{
            return R.error();
        }

    }

    //根据id查询讲师
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id){
        //根据id查询讲师记录
        EduTeacher teacher = eduTeacherService.getById(id);

        //返回
        return R.ok().data("teacher",teacher);
    }

    //根据id修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        //根据id修改讲师记录
        boolean result = eduTeacherService.updateTeacherById(teacher);
        //判断结果并返回
        if(result){
            return R.ok();
        }else{
            return R.error();
        }

    }
}

